<?php

class EwalletController extends MemberController {

    public function actionSummary() {

        $criteria = new CDbCriteria;

        $criteria->condition = "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'";
        $model = sys_ewallet::model()->find($criteria);



        $data['saldo_ewallet_utama'] = $model->ewallet_cash_acc - $model->ewallet_cash_paid;
        $data['saldo_ewallet_produk'] = $model->ewallet_products_acc - $model->ewallet_products_paid;
        $data['saldo_ewallet_voucher'] = $model->ewallet_voucher_acc - $model->ewallet_voucher_paid;

        $this->render('summary', array(
            'data' => $data,
        ));
    }

    public function actionLog() {

        $segment = function_lib::uri_segment(4);

        $ewallet_log_type = (empty($segment)) ? "cash" : $segment;

        $date_start_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $date_end_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") , date("Y")));

        $date_start = (!empty($_POST['date_start'])) ? $_POST['date_start'] : $date_start_default;
        $date_end = (!empty($_POST['date_end'])) ? $_POST['date_end'] : $date_end_default;
        //die("$date_start # $date_end");
        $criteria = new CDbCriteria;

        //$criteria->condition = "ewallet_log_network_id = '" . Yii::app()->user->getState('network_id') . "'";
        //$criteria->condition = "ewallet_log_datetime >= '$date_start 00:00:00'";
        //$criteria->condition = "ewallet_log_datetime <= '$date_end 00:00:00'";
        $criteria->condition = "DATE(ewallet_log_datetime) BETWEEN '" . $date_start . "' AND '" . $date_end . "'
                 AND ewallet_log_type = '" . $ewallet_log_type . "'
                 AND ewallet_log_network_id = '" . Yii::app()->user->getState('network_id') . "'";

        //$criteria->condition = "ewallet_log_type = '$ewallet_log_type'";
        $criteria->order = "ewallet_log_datetime ASC";

        $model = sys_ewallet_log::model()->findAll($criteria);

        $this->render('log', array(
            'model' => $model,
            'date_start' => $date_start,
            'date_end' => $date_end,
        ));
    }

    public function actionLogwithdraw() {

        $date_start_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        //$date_end_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
        $date_end_default = date("Y-m-d");

        $date_start = (!empty($_POST['date_start'])) ? $_POST['date_start'] : $date_start_default;
        $date_end = (!empty($_POST['date_end'])) ? $_POST['date_end'] : $date_end_default;
        //die("$date_start # $date_end");
        $criteria = new CDbCriteria;

        $criteria->condition = "ewallet_withdrawal_network_id = '" . Yii::app()->user->getState('network_id') . "'
                AND DATE(ewallet_withdrawal_datetime) BETWEEN '" . $date_start . "' AND '" . $date_end . "'";
        //$criteria->condition = "ewallet_log_datetime >= '$date_start 00:00:00'";
        //$criteria->condition = "ewallet_log_datetime <= '$date_end 00:00:00'";
        //$criteria->condition = "ewallet_withdrawal_datetime between '$date_start 00:00:00' AND '$date_end 00:00:00'";
        $criteria->order = "ewallet_withdrawal_datetime ASC";

        $model = sys_ewallet_withdrawal::model()->findAll($criteria);

        $this->render('logwithdraw', array(
            'model' => $model,
            'date_start' => $date_start,
            'date_end' => $date_end,
        ));
    }

    public function actionWithdrawal() {

        $criteria = new CDbCriteria;

        $criteria->condition = "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'";
        $ewallet = sys_ewallet::model()->find($criteria);

        $ewallet_withdraw = new sys_ewallet_withdrawal;
        $ewallet_log = new sys_ewallet_log;


        $saldo_ewallet = $ewallet->ewallet_cash_acc - $ewallet->ewallet_cash_paid;
        //cek saldo ewallet
        $model = new withdraw_ewallet_form;
        if ($_POST) {
            if ($model->validate()) {
                //kurangi saldo ewallet
                $ewallet->ewallet_cash_paid = ( $ewallet->ewallet_cash_paid + $_POST['withdraw_ewallet_form']['jumlah_ditransfer']);
                $ewallet->save();
                //masukan data ewallet_withdraw 
                $ewallet_withdraw->ewallet_withdrawal_network_id = Yii::app()->user->getState('network_id');
                $ewallet_withdraw->ewallet_withdrawal_value = $_POST['withdraw_ewallet_form']['jumlah_ditransfer'];
                $ewallet_withdraw->ewallet_withdrawal_datetime = date("Y-m-d H:i:s");
                $ewallet_withdraw->save();
                //masukan log ewallet
                $ewallet_log->ewallet_log_network_id = Yii::app()->user->getState('network_id');
                $ewallet_log->ewallet_log_type = "cash";
                $ewallet_log->ewallet_log_value = $_POST['withdraw_ewallet_form']['jumlah_ditransfer'];
                $ewallet_log->ewallet_log_status = "debit";
                $ewallet_log->ewallet_log_datetime = date("Y-m-d H:i:s");
                $ewallet_log->ewallet_log_note = "Withdraw Ewallet";
                $ewallet_log->save();

                $this->refresh();

                Yii::app()->user->setFlash('message', 'Witdraw Ewallet sebesar ' . $_POST['withdraw_ewallet_form']['jumlah_ditransfer'] . ' Sukses');
            }
        }

        $this->render('withdrawal', array(
            "model" => $model,
            "saldo_ewallet" => $saldo_ewallet,
        ));
    }

    public function actionTopup() {

        $model = new topup_ewallet_form;
        if ($_POST) {
            $post_serial = $_POST['serial_pin_part1'];
            $serial_topup = sys_serial_topup::model()->findByPk($post_serial);

            $topup_value = $serial_topup->serial_topup_value;

            if ($model->validate()) {
                //echo "ok dab";
                //update serial topup
                $serial_topup->serial_topup_is_used = '1';
                $serial_topup->save();
                //insert topup
                $ewallet_topup = new sys_ewallet_topup;
                $ewallet_topup->ewallet_topup_network_id = Yii::app()->user->getState('network_id');
                $ewallet_topup->ewallet_topup_serial_topup_id = $post_serial;
                $ewallet_topup->ewallet_topup_value = $topup_value;
                $ewallet_topup->ewallet_topup_datetime = date("Y-m-d H:i:s");
                $ewallet_topup->save();
                //update saldo ewallet
                $criteria = new CDbCriteria;

                $criteria->condition = "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'";
                $ewallet = sys_ewallet::model()->find($criteria);
                $ewallet->ewallet_cash_acc = ( $ewallet->ewallet_cash_acc + $topup_value);
                $ewallet->save();
                //isert log
                $ewallet_log = new sys_ewallet_log;
                $ewallet_log->ewallet_log_network_id = Yii::app()->user->getState('network_id');
                $ewallet_log->ewallet_log_type = "cash";
                $ewallet_log->ewallet_log_value = $topup_value;
                $ewallet_log->ewallet_log_status = "credit";
                $ewallet_log->ewallet_log_datetime = date("Y-m-d H:i:s");
                $ewallet_log->ewallet_log_note = "Topup Ewallet sn: $post_serial ";
                $ewallet_log->save();
                
                Yii::app()->user->setFlash('message', 'Topup Ewallet Sukses');

            }
        }
        $this->render('topup', array(
            "model" => $model,
        ));
    }

    public function actionWithdrawal_report() {
        $date_start_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $date_end_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));

        $date_start = (!empty($_POST['date_start'])) ? $_POST['date_start'] : $date_start_default;
        $date_end = (!empty($_POST['date_end'])) ? $_POST['date_end'] : $date_end_default;

        $criteria = new CDbCriteria;

        $criteria->condition = "ewallet_withdrawal_network_id = '" . Yii::app()->user->getState('network_id') . "'";
        $criteria->condition = "ewallet_withdrawal_datetime >= '$date_start'";
        $criteria->condition = "ewallet_withdrawal_datetime <= '$date_end'";
        $criteria->order = "ewallet_withdrawal_datetime ASC";

        $model = sys_ewallet_withdrawal::model()->findAll($criteria);

        $this->render('withdrawal_report', array(
            'model' => $model,
            'date_start' => $date_start,
            'date_end' => $date_end,
        ));
    }

    public function actionTransfer() {

        $criteria = new CDbCriteria;

        $criteria->condition = "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'";
        $ewallet = sys_ewallet::model()->find($criteria);

        $ewallet_log_credit = new sys_ewallet_log;
        $ewallet_log_debit = new sys_ewallet_log;

        $saldo_ewallet = $ewallet->ewallet_cash_acc - $ewallet->ewallet_cash_paid;
        //cek saldo ewallet
        $model = new withdraw_ewallet_form;
        if ($_POST) {
            if ($model->validate()) {
                //kurangi saldo ewallet
                $ewallet->ewallet_cash_paid = ( $ewallet->ewallet_cash_paid + $_POST['withdraw_ewallet_form']['jumlah_ditransfer']);
                if( $_POST['withdraw_ewallet_form']['transfer_to'] == "products" ){
                    $ewallet->ewallet_products_acc = ( $ewallet->ewallet_products_acc + $_POST['withdraw_ewallet_form']['jumlah_ditransfer']);
                } else {
                    $ewallet->ewallet_voucher_acc = ( $ewallet->ewallet_voucher_acc + $_POST['withdraw_ewallet_form']['jumlah_ditransfer']);
                }
                $ewallet->save();

                //masukan log ewallet
                $ewallet_log_debit->ewallet_log_network_id = Yii::app()->user->getState('network_id');
                $ewallet_log_debit->ewallet_log_type = "cash";
                $ewallet_log_debit->ewallet_log_value = $_POST['withdraw_ewallet_form']['jumlah_ditransfer'];
                $ewallet_log_debit->ewallet_log_status = "debit";
                $ewallet_log_debit->ewallet_log_datetime = date("Y-m-d H:i:s");
                $ewallet_log_debit->ewallet_log_note = "Transfer Ewallet ke Ewallet Produk";
                $ewallet_log_debit->save();

                //masukan log ewallet
                $ewallet_log_credit->ewallet_log_network_id = Yii::app()->user->getState('network_id');
                if( $_POST['withdraw_ewallet_form']['transfer_to'] == "products" ){
                    $ewallet_log_credit->ewallet_log_type = "products";
                } else {
                    $ewallet_log_credit->ewallet_log_type = "voucher";
                }    
                $ewallet_log_credit->ewallet_log_value = $_POST['withdraw_ewallet_form']['jumlah_ditransfer'];
                $ewallet_log_credit->ewallet_log_status = "credit";
                $ewallet_log_credit->ewallet_log_datetime = date("Y-m-d H:i:s");
                $ewallet_log_credit->ewallet_log_note = "Transfer Dari Ewallet Cash ";
                $ewallet_log_credit->save();

                $this->refresh();
                Yii::app()->user->setFlash('message', 'Transfer Ewallet sebesar ' . $_POST['withdraw_ewallet_form']['jumlah_ditransfer'] . ' Sukses');
            }
        }

        $this->render('transfer', array(
            "model" => $model,
            "saldo_ewallet" => $saldo_ewallet,
        ));
    }

}
